package com.xinkao.log.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.xinkao.log.entity.Log;
import com.xinkao.log.entity.Page;
import com.xinkao.log.service.LogService;
import com.xinkao.log.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 周洋
 * @Description
 * @createTime 2021/9/27 16:07
 */
@RestController
@RequestMapping("/log")
public class LogController {

    @Autowired
    private LogService logService;

    /**
     * 列表
     * @param requestJson
     * @return
     */
    @PostMapping("/listLog")
    public JSONObject listRevLog(HttpServletRequest request, @RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "pageNum,pageRow");
        //系统标识
        String systemMark = requestJson.getString("systemMark");
        //操作人
        String operator = requestJson.getString("operator");
        //操作类别
        String operate = requestJson.getString("operate");
        //操作开始日期
        String operatingStartTime = requestJson.getString("operatingStartTime");
        //操作结束日期
        String operatingEndTime = requestJson.getString("operatingEndTime");
        //分页
        Integer pageNum = requestJson.getInteger("pageNum");
        Integer pageRow = requestJson.getInteger("pageRow");
        Page<Log> page = new Page<>(pageNum, pageRow);
        //查询条件
        Query query  = new Query();
        Criteria criteria = Criteria.where("systemMark").is(systemMark);
        if(!StrUtil.isEmptyIfStr(operator)){
            criteria.and("operator").regex(".*"+operator+".*");
        }
        if(!StrUtil.isEmptyIfStr(operate)){
            criteria.and("operate").regex(".*"+operate+".*");
        }
        if(!StrUtil.isEmptyIfStr(operatingStartTime) && !StrUtil.isEmptyIfStr(operatingEndTime)){
            criteria.and("operatingTime").gte(operatingStartTime).lte(operatingEndTime);
        }
        query.addCriteria(criteria);

        Page<Log> logPage = logService.findPage(page, query);

        //把分页数据获取出来，放到map集合
        JSONObject json = new JSONObject();
        json.put("list", logPage.getRows());
        json.put("totalCount", logPage.getTotal());

        return CommonUtil.successJson(json);
    }
}
